#include "openGLVolumeRenderer.h"

#include <GL/glew.h> //inorder to get all lib setup,
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "glu32.lib")

#include <iostream>

//pure virtual destructor of the base class
volumeEngineCoreInterface::~volumeEngineCoreInterface(){}


openGLVolumeRenderer::openGLVolumeRenderer()
{
		std::cout<<"Render in dll"<< std::endl;

}
//virtual destructor
openGLVolumeRenderer::~openGLVolumeRenderer()
{
	std::cout<<"openGLRenderer destructor"<<std::endl;
}

void openGLVolumeRenderer::destroyObject()
{
	delete this;
}

bool openGLVolumeRenderer::InitEngine(int w, int h)
{
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	bool isOK = true;
	_width = w; _height = h;


	isOK &= createFBO();
	isOK &= createProgram();
	isOK &= createTF();

	//tex = new GLTexture("head256X256X256.raw",1,256,256,256);
	//tex->LoadTexture();

	return isOK;
}

void openGLVolumeRenderer::Render()
{
	//setupOpenGLPerspectView(_width, _height, _paraV.rotateMatrix, _paraV.posVec);
	
}

void openGLVolumeRenderer::RenderToImage()
{
}

void openGLVolumeRenderer::Resize(int w, int h) 
{
	_width = w; _height = h;
	createFBO();


}

void openGLVolumeRenderer::Refresh() // reload shader etc.
{
}

void openGLVolumeRenderer::Init1DTF(int dataSize, int tableSize)
{

}

void openGLVolumeRenderer::Update1DTF(void* buffer=0)
{
}

void openGLVolumeRenderer::SetViewingParameters(parameterViewing &paraV)
{
	_paraV = paraV;
}
void openGLVolumeRenderer::SetShadingParameters(parameterShading &paraS)
{
	_paraS = paraS;
}
bool openGLVolumeRenderer::LoadCoreStatusFromFile(const char* filename)
{
	return false;
}

bool openGLVolumeRenderer::SaveCoreStatusToFile(const char* filename)
{
	return false;
}

bool openGLVolumeRenderer::loadSingleVolume(const char* filename)
{
	return false;
}

//==========================
bool openGLVolumeRenderer::createFBO()
{
	//create texture
	//texture backface_buffer
	glGenTextures(1, &m_backface);
	glBindTexture(GL_TEXTURE_2D, m_backface);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA16F_ARB, _width, _height, 0, GL_RGBA, GL_FLOAT, NULL);
	
	//texture m_raycast
	glGenTextures(1, &m_raycast);
	glBindTexture(GL_TEXTURE_2D, m_raycast);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA16F_ARB, _width, _height, 0, GL_RGBA, GL_FLOAT, NULL);

	fboBackFace = new FramebufferObject();
	//fbo1->Bind();
	fboBackFace->AttachTexture(GL_TEXTURE_2D, m_backface,GL_COLOR_ATTACHMENT0_EXT,0,0);
    if(!fboBackFace->IsValid())
		std::cout<<"fbo not valid"<<std::endl;
    fboBackFace->Disable();

	fboRaycast = new FramebufferObject();
	//fbo2->Bind();
	fboRaycast->AttachTexture(GL_TEXTURE_2D, m_raycast,GL_COLOR_ATTACHMENT0_EXT,0,0);
    if(!fboRaycast->IsValid())
		std::cout<<"fbo not valid"<<std::endl;
    fboRaycast->Disable();
	
	return true;
}

bool openGLVolumeRenderer::createProgram()
{
	return true;
}

bool openGLVolumeRenderer::createTF()
{
	return true;
}




